<?php

namespace yak\layui\search;

use yak\layui\models\admin\User as Admin;
use yak\layui\traits\SearchTrait;
use yak\layui\widgets\grid\LayuiActionColumn;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * AdminSearch represents the model behind the search form of `yak\layui\models\Admin`.
 */
class AdminSearch extends Admin implements LayuiSearchInterface

{
    use SearchTrait;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'status', 'type'], 'integer'],
            [['name', 'avatar', 'username', 'auth_key', 'password_hash', 'password_reset_token', 'email'], 'safe'],
            [['yak_lst', 'yak_lst_txt', 'yak_lst_ext', 'yak_dt', 'yak_dt_s', 'yak_dt_e'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Admin::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params, '');

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        if (!empty($this->yak_dt_s) && !empty($this->yak_dt_e)) {
            $this->yak_dt_e = date('Y-m-d', strtotime('+1 day', strtotime($this->yak_dt_e)));
            $query->andFilterWhere(['between', $this->yak_dt, $this->yak_dt_s, $this->yak_dt_e]);
        }

        if (!empty($this->yak_lst)) {

            if ($this->yak_lst_ext == 'EQ') {
                $query->andFilterWhere([$this->yak_lst => $this->yak_lst_txt]);
            }

            if ($this->yak_lst_ext == 'LIKE') {
                $query->andFilterWhere(['like', $this->yak_lst, $this->yak_lst_txt]);
            }
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'type' => $this->type,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'avatar', $this->avatar])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
            ->andFilterWhere(['like', 'email', $this->email]);

        return $dataProvider;
    }


    /**
     * @return array
     */
    public function columns(): array
    {
        return [
            'id',
            'name',
            'username',
            'email',
            [
                'attribute' => 'status',
                'format' => ['selected', $this->statusLabels()]
            ],
            [
                'attribute' => 'type',
                'format' => ['selected', $this->typeLabels()]
            ],
            'created_at:datetime',
            'updated_at:datetime',
            [
                'class' => LayuiActionColumn::class,
                'toolbar' => '#tool',
                'options' => [
                    'colOptions' => [
                        'width' => 250,
                    ]
                ]
            ]
        ];
    }

    /**
     * @return array
     */
    public function limits(): array
    {
        // TODO: Implement limits() method.
        return [10, 20, 50, 100];
    }

    /**
     * @return int
     */
    public function pageSize(): int
    {
        // TODO: Implement pageSize() method.
        return 15;
    }
}
